
#include <stdio.h>

#include "command.h"

Command::Command(void)
{
}

Command::Command(int id)
{
  this->id = id;
}

Command::Command(int id, int value)
{
  this->id = id;
  setInt(value);
}

Command::Command(int id, float value)
{
  this->id = id;
  setFloat(value);
}

Command::Command(int id, bool value)
{
  this->id = id;
  setBool(value);
}

Command::Command(SerialPortPackage serial_port_package)
{
  id = serial_port_package.id;
  payload = serial_port_package.message;
}

float Command::getFloat(void) const
{
  float value;
  sscanf(payload.c_str(), "%f", &value);
  return value;
}

void Command::setFloat(float value)
{
  char buffer[20];
  sprintf(buffer, "%f", value);
  payload = buffer;
}

int Command::getInt(void) const
{
  int value;
  sscanf(payload.c_str(), "%d", &value);
  return value;
}

void Command::setInt(int value)
{
  char buffer[15];
  itoa(value, buffer, 10);
  payload = buffer;
}

bool Command::getBool(void) const
{
  return payload.compare("true");  
}

void Command::setBool(bool value)
{
  if (value) {
    payload = "true";
  } else {
    payload = "false";
  }
}
